﻿using IBaseRepository;
using SqlSugarAndEntity.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Services.Base;
using Common.ExpressionHelper;
using Common.CommonModel;
using Core.AutoFacService.Attributes.InjectAttributes;

namespace Services.Org
{
    [Inject]
    public class OrgService:BaseService<sys_orginfo>, IOrgService
    {
        private IOrgRepository orgRepository;
        public OrgService(IOrgRepository orgRepository) :base(orgRepository)
        {
            this.orgRepository = orgRepository;
        }

        public ResponseModel GetLazyOrgTree(decimal? ParentId, int page = 1, int limit = 1000)
        {
            return CreateResponseModel(orgRepository.GetLazyOrgTree, ParentId, page, limit);
        }


        public ResponseModel GetOrgTree()
        {
            ResponseModel res = new ResponseModel();
            var list = ExpressionHelper.ConvertToTreeList(orgRepository.GetInfo(t=>true), t => t.ParentOrgID == 0 || t.ParentOrgID == null, t => t.ID ==t.ParentOrgID );
            res.code = ResponseTypeEnum.GetInfoSucess;
            res.message = ResponseTypeEnum.GetInfoSucess;
            res.model = list;
            return res;  
        }
        public List<decimal> GetOrgChildrenByID(decimal ParentId)
        {
            //获取全部的组织树
            var totalList = orgRepository.GetInfo(t => true);
            return ExpressionHelper.GetTargetStreamValue(totalList,t=>t.ID== ParentId,t=>t.ID,t=> t.ID==t.ParentOrgID );
        }
    }
}
